home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / prolog / modprolg / mod-prol.lha / Prolog / modlib / src / $readloop.P < prev    next >
Encoding:
Text File  |  1992-05-27  |  14.7 KB  |  453 lines

  1. /************************************************************************
  2. *                                    *
  3. * The SB-Prolog System                            *
  4. * Copyright SUNY at Stony Brook, 1986; University of Arizona, 1987    *
  5. *                                    *
  6. ************************************************************************/
  7.  
  8. /*-----------------------------------------------------------------
  9. SB-Prolog is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY.  No author or distributor
  11. accepts responsibility to anyone for the consequences of using it
  12. or for whether it serves any particular purpose or works at all,
  13. unless he says so in writing.  Refer to the SB-Prolog General Public
  14. License for full details.
  15.  
  16. Everyone is granted permission to copy, modify and redistribute
  17. SB-Prolog, but only under the conditions described in the
  18. SB-Prolog General Public License.   A copy of this license is
  19. supposed to have been given to you along with SB-Prolog so you
  20. can know your rights and responsibilities.  It should be in a
  21. file named COPYING.  Among other things, the copyright notice
  22. and this notice must be preserved on all copies. 
  23. ------------------------------------------------------------------ */
  24.  
  25. /****************************************************************************
  26.  *                                                                          *
  27.  * This file has been changed by to include Modules Extensions              *
  28.  * Changes by : Brian Paxton 1991/92                                        *
  29.  * Last update : June 1992                                                  *
  30.  *                                                                          *
  31.  * Organisation : University of Edinburgh.                                  *
  32.  * For : Departments of Computer Science and Artificial Intelligence        * 
  33.  *       Fourth Year Project.                                               *
  34.  *                                                                          *
  35.  ****************************************************************************/
  36.  
  37. /* $readloop.P */
  38.  
  39. /* This is the top-level read-eval-print loop for Prolog. 
  40.  
  41.    $readloop also sets up some global values to allow it to handle
  42.    break levels. It defines break/0 and abort/0. (repeat/0 is also
  43.    defined here, for lack of a better place for now.)
  44.  
  45. $readl_export([break/0,abort/0,repeat/0]).
  46.  */
  47.  
  48. $readloop :- 
  49.     $init_sys,
  50.     $load_mod($io),
  51.     $load_mod($read),
  52.     $load($prorc),call($prorc),
  53.     $globalset('_$abort_cutpoint'(0)),
  54.     $globalset('_$break_level'(0)),
  55.     $writename('MODULAR SB-PROLOG Version '),
  56.     $version(V), $writename(V), $nl,
  57.     $setup_modules,
  58.     $readlp1.
  59.  
  60. '_$tab_size'(17).
  61.  
  62. % Input is now read with respect to the root module :
  63. %          $read(X, Vars, 0)    
  64. % and predicates which require an extra structure tag argument are extended
  65. % using the root structure tag :
  66. %          $expand_body(X,Y,0)
  67.  
  68. $readlp1 :- repeat,'_$savecp'(Cp),
  69.     $globalset_a('_$abort_cutpoint'(Cp),Cp),
  70.     $flags(2,0), /* set flag to intercept stack overflow */
  71.     $globalset('_$break_level'(0)),
  72.     $readl_userio(I,O),
  73.     $writename('| ?- '),        % Old version :
  74.     $read(X, Vars, 0),        %   $read(X, Vars),
  75.     $expand_body(X,Y,0),        %   $expand_body(X,Y),
  76.     $readl_resetio(I,O),
  77.     ($readl_stop(Y),!;$readl_procq(Y,Vars)).
  78.  
  79. $readl_stop(halt) :- halt. /* quit entire system, regardless of break level */
  80. $readl_stop(end_of_file).    /* pop a break level, out if at top */
  81.  
  82. $readl_procq(X,[]) :- !,$readl_docall(X),!,
  83.     $readl_userio(I,O),$writename(yes),$nl,$readl_resetio(I,O),fail.
  84.  
  85. $readl_procq(X,Vars) :- $readl_docall(X),
  86.     $readl_userio(I,O),
  87.     $readl_printans(Vars),
  88.     $readl_ifnomo(I,O),!,$writename(yes),$nl,$readl_resetio(I,O),fail.
  89.  
  90. $readl_ifnomo(_,_) :- $get0(10),!.
  91. $readl_ifnomo(I,O) :- $readl_ifnomo(I,O),$readl_resetio(I,O),fail.
  92.  
  93. $readl_docall(X) :- call(X).
  94. $readl_docall(_) :- $readl_userio(I,O),$writename(no),$nl,
  95.     $readl_resetio(I,O),fail.
  96.  
  97. $readl_printans([]) :- !.
  98. $readl_printans([=(Name, Val)|Tail]) :-
  99.         $nl,$writename(Name),
  100.         $writename(' = '),
  101.         $check_hidden(Val,Newval),
  102.         $write(Newval),
  103.         $readl_printans(Tail).
  104.  
  105. break :- '_$break_level'(Blevel),
  106.     Nblevel is Blevel+1, $globalset('_$break_level'(Nblevel)),
  107.     $readl_userio(I,O),
  108.     $writename('[ Break (level '),$writename(Nblevel),
  109.     $writename(') ]'),$nl,
  110.     $readl_brklp1,
  111.     $globalset('_$break_level'(Blevel)),
  112.     $writename('[ End break (level '),$writename(Nblevel),
  113.     $writename(') ]'),$nl,
  114.     $readl_resetio(I,O).    /* should we reset here ? */
  115.  
  116. $readl_brklp1 :- repeat,'_$break_level'(Blevel),
  117.     $writename(Blevel),$writename(': ?- '),
  118.     $read(X, Vars, 0),        %   $read(X, Vars),
  119.     $expand_body(X,Y,0),        %   $expand_body(X,Y),
  120.     ($readl_stop(Y),!;$readl_procq(Y,Vars)).
  121.  
  122. abort :- '_$abort_cutpoint'(Cp), '_$cutto'(Cp), fail.
  123.  
  124. $readl_userio(Oi,Oo) :- 
  125.     $seeing(Oi),$see(user),
  126.     $telling(Oo),$tell(user).
  127.  
  128. $readl_resetio(Oi,Oo) :- $see(Oi),$tell(Oo).
  129.  
  130. repeat.
  131. repeat :- repeat.
  132.  
  133. $flags(Which,What) :- '_$builtin'(113).
  134.  
  135.  
  136. % $check_hidden/2
  137. %
  138. % Converts any hidden predicates/functions in the result of a call to '...',
  139. % leaving the rest intact.
  140.  
  141. $check_hidden(X,X) :-
  142.     ( number(X) ; var(X) ; $is_buffer(X) ), !.
  143. $check_hidden([],[]) :- !.
  144. $check_hidden([Head|Tail],[NHead|NTail]) :- !,
  145.     $check_hidden(Head,NHead),
  146.     $check_hidden(Tail,NTail).
  147. $check_hidden(Term,Nterm) :-
  148.     $arity(Term,Arity),
  149.     $univ(Term,[Name|Args]),
  150.     $module_structure(root,0,_,Preds,Funs),
  151.     ( ( $memberchk(_/Arity ---> Name, Funs) ;
  152.         $memberchk(_/Arity ---> Name, Preds) ;
  153.         $dismantle_name(Name,_,0) ;
  154.         $dismantle_name(Name,_,perv) ) ->
  155.               ( $check_hidden(Args,Nargs),
  156.            $univ(Nterm,[Name|Nargs]) ) ;
  157.          Nterm = '...') ,!.
  158.  
  159. /* These are the module handling routines. The main routine is the
  160.    undefined_pred interrupt handler. The system keeps a table of modules
  161.    and routines that are needed from each. When a predicate is found to
  162.    be undefined, the table is searched to see if it is defined by 
  163.    some module. If so, that module is loaded (if it hasn't been previously
  164.    loaded) and the association is made between the routine name as 
  165.    defined in the module, and the routine name as used by the invoker.
  166.  
  167.    The table of modules and needed routines is:
  168.    defined_mods(modname,[pred1/arity1,...,predn/arityn]).
  169.      where modname is the name of the module. The module exports n
  170.     predicate definitions. The first exported pred is of arity 
  171.     arity1, and needs to be invoked by the name of pred1.
  172.  
  173.    The table of modules that have already been loaded is:
  174.    loaded_mods(modname).
  175.  
  176.    A module is a file of predicate definitions. Consider a module name
  177.    `mod'. It contains a single fact, named mod_export, that is true of
  178.    the list of predicate/arity's that are exported. 
  179.    e.g. mod_export([mod_p1/2,mod_p2/4]).
  180.    For each module, m, which contains predicates needed by this module,
  181.    there is a mod_use fact, describing what module is needed and the names
  182.    of the predicates defined there that are needed. For example, if module
  183.    mod needs to import predicates from mod m, there would be a fact:
  184.    mod_use(m,[mod_ip1/2,mod_ip2/4]), where m is a module that exports two
  185.    predicates: one 2-ary and one 4-ary. This list corresponds to the export
  186.    list of module m.
  187.  
  188. */
  189. $init_sys_export([$define_mod/2,$load/1,$load_mod/1]).
  190.  
  191. $init_sys :- 
  192.     $load($opcode),    /* THIS FILE MUST BE THE FIRST LOADED */
  193.     $load($modules),
  194.     $load($assert),
  195.     $load($db),
  196.     $load($dbcmpl),
  197.     $load($bio),
  198.     $load($bmeta),
  199.     $load($name),
  200.     $load($blist),
  201.     $load($call),
  202.     $load($glob),
  203.     $load($funrel),
  204.     $assert_union(loaded_mods(_),$loaded_mods(_)), /* nec for compile */
  205.     $assert_abolish_i(defined_mods(_,_)),    /* nec for compile [a] */
  206.     $globalset('_$nofile_msg'(1)).
  207.  
  208. $loaded_mods($buff).      /* with readloop */
  209. $loaded_mods($init_sys).  /* with readloop */
  210. $loaded_mods($assert).
  211. $loaded_mods($db).
  212. $loaded_mods($dbcmpl).
  213. $loaded_mods($bio).
  214. $loaded_mods($bmeta).
  215. $loaded_mods($name).
  216. $loaded_mods($blist).
  217. $loaded_mods($call).
  218. $loaded_mods($glob).
  219. $loaded_mods($funrel).
  220. $loaded_mods($modules).
  221.  
  222. $define_mod(Mod,Implist) :- defined_mods(Mod,Implist),!. /* no-op if there */
  223. $define_mod(Mod,Implist) :- $assert(defined_mods(Mod,Implist)).
  224.  
  225. /* the undefined_pred interrupt handler: */
  226.  
  227. '_$undefined_pred'(Term) :-
  228.     $functor0(Term,Pred),
  229.     $arity(Term,Arity),
  230.     ('_$nodynload'(Pred,Arity) ->
  231.         fail ;
  232.         (not(not('_$definable'(Term))),'_$call'(Term))
  233.     ).
  234.  
  235. '_$nodynload'(_,_) :- fail.
  236.  
  237. '_$definable'(Term) :- 
  238.     $functor0(Term,Pred),$arity(Term,Arity),
  239.     defined_mods(Module_name,Pred_name_list),  /* for each module.. */
  240.     $init_membernv(Pred/Arity,Pred_name_list), 
  241.         /* is needed pred in this one? */
  242.     !,                /* yes! */
  243.     $load_mod(Module_name),        /* go load it if necessary */
  244.     $name(Module_name,Mod_name_chars),
  245.     $append(Mod_name_chars,"_export",Exp_name_chars),
  246.     $name(Exp_name,Exp_name_chars),
  247.     $bldstr(Exp_name,1,Modcall),arg(1,Modcall,Explist),
  248.     '_$call'(Modcall),
  249.     $load_preds(Explist,Pred_name_list,Module_name). 
  250.  
  251. '_$definable'(Term) :-    /* no module to define pred */
  252.     $functor0(Term,Pred),
  253.     $dismantle_name(Pred,_,perv),  /* only load pervasive preds */
  254.     $load(Pred),!,        /* file exists */
  255.     (not($pred_undefined(Term))    /* file defines pred */
  256.      ;
  257.      $arity(Term,Arity),
  258.      $telling(Fi),$tell(user),
  259.      $writename(Pred),$writename('/'),$writename(Arity),
  260.      $writename(' not defined by file'),$nl,$tell(Fi),fail
  261.     ).
  262.  
  263. '_$definable'(Term) :- 
  264.     '_$nofile_msg'(1),    /* if message is to be displayed */
  265.     $telling(Fi),$tell(user),
  266.     $writename('Undefined predicate '),
  267.     $functor0(Term,Pred),$arity(Term,Arity),
  268.     $writename(Pred),$writename('/'),$writename(Arity),$nl,$tell(Fi),fail.
  269.  
  270. $load_mod(Module_name) :-
  271.     loaded_mods(Module_name),!.    /* already loaded, noop */
  272. $load_mod(Module_name) :-
  273.     $load(Module_name),!,         /* now loaded */
  274.     $assert(loaded_mods(Module_name)).
  275. $load_mod(Module_name) :-        /* no such file */
  276.     $telling(Fi),$tell(user),
  277.     $writename('No file to define module '),
  278.     $writename(Module_name),$nl,$tell(Fi),fail.
  279.  
  280. $load_preds([],[],_) :- !.
  281. $load_preds([],[_|_],Mod) :- !,
  282.     $telling(Fi),$tell(user),
  283.     $writename('Illegal use list for module: '),
  284.     $writename(Mod),$nl,$tell(Fi),fail.
  285. $load_preds([_|_],[],Mod) :- !,
  286.     $telling(Fi),$tell(user),
  287.     $writename('Illegal use list for module: '),
  288.     $writename(Mod),$nl,$tell(Fi),fail.
  289. $load_preds([Inname|Explist],[Inname|Pred_name_list],Mod) :- !,
  290.     $load_preds(Explist,Pred_name_list,Mod).
  291. $load_preds([Inname/Arity|Explist],[Outname/Arity|Pred_name_list],
  292.         Mod) :- !,
  293.     $bldstr(Outname,Arity,Outstr),$bldstr(Inname,Arity,Instr),
  294.     ($pred_undefined(Outstr),!,$assert_union(Outstr,Instr),
  295.         $load_preds(Explist,Pred_name_list,Mod)
  296.     ;
  297.         $telling(Fi),$tell(user),
  298.         $writename('Attempt to redefine '),$writename(Outname),
  299.         $writename('/'),$writename(Arity),$nl,
  300.         $tell(Fi),
  301.         $load_preds(Explist,Pred_name_list,Mod)
  302.     ).
  303. $load_preds([Inname/Inarity|Explist],[Outname/Outarity|Pred_name_list],
  304.         Mod) :-
  305.     $telling(Fi),$tell(user),
  306.     $writename('Incorrect import arity: '),$writename(Outname),
  307.     $writename('/'),$writename(Outarity),$nl,
  308.     $tell(Fi),
  309.     $load_preds(Explist,Pred_name_list,Mod),
  310.     fail.
  311.  
  312.  
  313. $init_membernv(Pa,[Tpa|_]) :- nonvar(Tpa),Pa=Tpa.
  314. $init_membernv(Pa,Pn_list) :- nonvar(Pn_list),
  315.     Pn_list=[_|Pn_tail],$init_membernv(Pa,Pn_tail).
  316.  
  317.  
  318. /* the keyboard interrupt handler */
  319.  
  320. '_$keyboard_int'(Call) :- break,'_$call'(Call).
  321.  
  322. /* the general interrupt handler! */
  323.  
  324. '_$interrupt'(Call,Code) :-
  325.     Code =:= 0 -> '_$undefined_pred'(Call);
  326.       (Code =:= 1 -> '_$keyboard_int'(Call);
  327.           (Code =:= 2 -> 
  328.         $tell(user),$writename('Stack Overflow!!'),$nl,abort
  329.         ;
  330.         $writename('Illegal interrupt code'),halt
  331.           )
  332.       ).
  333.  
  334. /********************************************************/
  335. /* This is the dynamic load routine. It gets the SIMPATH global variable
  336. and uses it to determine what files to try to load. */
  337.  
  338. load(File) :- $load(File).    /* for now */
  339. $load(File) :- $buff_code(File, 0, 31 /*gb*/,0'/) -> $loadqual(File,0);
  340.             not(not($nnload(File))).
  341.  
  342. $loadqual(File,Rc) :- '_$builtin'(11).
  343.  
  344. $nnload(File) :- 
  345.     $conlength(File,Flen),
  346.     $readl_simpath(Dir),
  347.     $conlength(Dir,Dlen),Wlen is Flen+Dlen+1,
  348.     $alloc_heap(Wlen,Wname),
  349.     $substring(0,Dlen,Dir,0,Wname,Dlen),
  350.     $substring(0,1,'/',Dlen,Wname,Floc),
  351.     $substring(0,Flen,File,Floc,Wname,Wlen),
  352.     $loadqual(Wname,0),! .
  353.  
  354. $readl_simpath(Dir) :- 
  355.     $getenv_simpath(Simpath),
  356.     $readl_getenvdir(Simpath,0,Dir).
  357.  
  358. $readl_getenvdir(Simpath,Loc,Dir) :- 
  359.     $subdelim(1,':',Dir1,Loc,Simpath,Nloc) ->
  360.         (Dir = Dir1;
  361.          $readl_getenvdir(Simpath,Nloc,Dir)
  362.         )
  363.     /* else */ ;
  364.         $conlength(Simpath,Len),Llen is Len-Loc,
  365.         $substring(1,Llen,Dir,Loc,Simpath,_).
  366.  
  367. /* get environment variable value */
  368.  
  369. $getenv_simpath(X) :- '_$builtin'(12).
  370.  
  371. /* $buff *******************************************************/
  372. /* needed for dynamic loader */
  373.  
  374. $buff_export([$alloc_perm/2,$alloc_heap/2,$trimbuff/3,$buff_code/4,$symtype/2,
  375.         $substring/6,$subnumber/6,$subdelim/6,$conlength/2,
  376.         $pred_undefined/1, $hashval/3]).
  377.  
  378. $alloc_perm(Size, Buff) :- $alloc_buff(Size,Buff,0,0,0).
  379.  
  380. $alloc_heap(Size, Buff) :- $alloc_buff(Size,Buff,1,0,0).
  381.  
  382. /* Type 0: perm, 1: heap, 2: from Supbuff */
  383. $alloc_buff(Size,Buff,Type,Supbuff,Retcode) :- 
  384.     $alloc_buff1(Size,Buff,Type,Supbuff,Retcode),
  385.     (Retcode =\= 0 -> 
  386.         $writename('alloc failed'),$nl,fail;
  387.         true).
  388. $alloc_buff1(Size,Buff,Type,Supbuff,Retcode) :- '_$builtin'(76).
  389.  
  390. $trimbuff(Size,Buff,Type) :- '_$builtin'(79).
  391. $trimbuff(Size,Buff,Type,Supbuff) :- '_$builtin'(79).
  392.  
  393. $buff_code(Buff,Offset,Disc,Term) :- '_$builtin'(77).
  394.  
  395. /* Type = 0: no ep, 1: dynamic, 2: ep to compiled code, 3: buffer */
  396. $symtype(Term,Type) :- '_$builtin'(42).
  397.  
  398. $substring(Dir,NumBytes,Const,Locin,Buff,Locout) :- '_$builtin'(51).
  399.  
  400. $subnumber(Dir,NumBytes,NumCon,Locin,Buff,Locout) :- '_$builtin'(52).
  401.  
  402. $subdelim(Dir,Delim,Const,Locin,Buff,Locout) :- '_$builtin'(53).
  403.  
  404. $conlength(Const,Len) :- '_$builtin'(54).
  405.  
  406. $pred_undefined(Term) :- 
  407.     $symtype(Term,D),
  408.     (D=:=0;
  409.      D=\=0,D=:=3).
  410.  
  411. $hashval(Arg, Size, Hashval) :- '_$builtin'(43).
  412.  
  413. /* These routines put numbers into buffers in internet format */
  414. $buff_putnum_n(Buff,Loc,Len,Num) :-
  415.     Len =< 1 -> 
  416.         $buff_code(Buff,Loc,30 /*pb*/ ,Num)
  417.     /* else */ ;
  418.         Byte is Num /\ 255,
  419.         Rest is Num >> 8,
  420.         Nlen is Len-1,Sloc is Loc+Nlen,
  421.         $buff_code(Buff,Sloc,30 /*pb*/ ,Byte),
  422.         $buff_putnum_n(Buff,Loc,Nlen,Rest).
  423.  
  424. $buff_getnum_n(Buff,Loc,Len,Num) :-
  425.     Len =< 1 -> 
  426.         $buff_code(Buff,Loc,31 /*gb*/ ,Num)
  427.     /*else*/ ;
  428.         Nlen is Len-1,Sloc is Loc+Nlen,
  429.         $buff_code(Buff,Sloc,31,Byte),
  430.         $buff_getnum_n(Buff,Loc,Nlen,Rest),
  431.         Num is (Rest << 8) + Byte.
  432.  
  433. $globalset_a(Place,Value) :- 
  434.     integer(Value) -> 
  435.          ($opcode( getnumcon, GetNumOp ),
  436.               $buff_code(Place, 0, 7, /*gepb*/ Buff),
  437.           $buff_code(Buff,  4, 3, /*ps*/   GetNumOp /*getnumcon*/),
  438.           $buff_code(Buff,  8, 2, /*pn*/   Value)
  439.          ) ;
  440.     (real(Value) ->
  441.          ($opcode( getfloatcon, GetFltOp ),
  442.               $buff_code(Place, 0, 7, /*gepb*/ Buff),
  443.           $buff_code(Buff,  4, 3, /*ps*/   GetFltOp /*getfloatcon*/),
  444.           $buff_code(Buff,  8,27, /*pf*/   Value)
  445.          )
  446.     ).
  447.  
  448. /* for debugging */
  449. $writename(X) :- '_$builtin'(133).
  450. $nl :- '_$builtin'(26).
  451.  
  452.  
  453.